package com.xiaolin.practice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaolin.practice.mapper.DepartmentMapper;
import com.xiaolin.practice.mapper.WorkOrderMapper;
import com.xiaolin.practice.mapper.WorkOrderMetricsMapper;
import com.xiaolin.practice.pojo.*;
import com.xiaolin.practice.service.WorkOrderService;
import com.zaxxer.hikari.HikariDataSource;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Service
public class WorkOrderImpl extends ServiceImpl<WorkOrderMapper, WorkOrder> implements WorkOrderService {
    @Autowired
    private WorkOrderMapper workOrderMapper;
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private WorkOrderMetricsMapper workOrderMetricsMapper;
    @Autowired
    private HikariDataSource dataSource;

    public boolean saveWorkOrder(WorkOrder workOrder) {
        //判断添加的数据是否有重复
        if (extracted(workOrder)) return true;
        workOrderMapper.insert(workOrder);
        return false;
    }

    private boolean extracted(WorkOrder workOrder) {
        for (WorkOrder order : findAll()) {
            if (workOrder.getOrderNo().equals(order.getOrderNo())) {
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean updateByWorkOrder(WorkOrder workOrder) {
        WorkOrder workOrder1 = workOrderMapper.selectById(workOrder.getId());
        if (!workOrder1.getOrderNo().equals(workOrder.getOrderNo()) ) {
            if (extracted(workOrder)) return true;
        }
        workOrderMapper.updateById(workOrder);
        return false;
    }

    @Override
    public IPage<WorkOrder> pageWorkOrder(Page<WorkOrder> p, Map<String, Object> workOrderMap) {
        return workOrderMapper.selectPage(p, null);
    }

    @Override
    public boolean fenpai(Long orderId, Long departmentId, String departmentName) {
        try{
            QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("dept_id", departmentId);
            //获取部门
            Department department = departmentMapper.selectOne(queryWrapper);
            if (department == null) return false;
            if (!department.getDeptName().equals(departmentName)) return false;
            WorkOrder workOrder = workOrderMapper.selectById(orderId);
            workOrder.setFenpaiTime(new Date());
            workOrderMapper.updateById(workOrder);

        }catch (Exception e){
            e.printStackTrace();
        }
        return true;
    }

    @Override
    public List<WorkOrderDays> samplingOverdue(String dateMM) {
        Integer days = null;
        int daysInMonth = getDaysInMonth(dateMM);
        days = daysInMonth;
//        System.out.println("7月的天数: " + daysInMonth); // 应该输出 31
        String dateMM1 = dateMM+"-01";
        String dateEnd = dateMM+"-"+(days+1);
        List<WorkOrderDays> workOrderMetrics = workOrderMetricsMapper.getWorkOrderMetrics(dateMM1,dateEnd);
        setOV(workOrderMetrics);
        return workOrderMetrics;
    }

    private static void setOV(List<WorkOrderDays> workOrderMetrics) {
        for (WorkOrderMetrics workOrderMetric : workOrderMetrics) {
                if (workOrderMetric.getTotalOrders() == 0){
                    workOrderMetric.setWorkOrderOV(0.00);
                }else {
                    double i = (double) Math.round((workOrderMetric.getTotalOverdueOrders() / workOrderMetric.getTotalOrders())*100) ;
                    workOrderMetric.setWorkOrderOV(i);
                }
        }
    }

    private int getDaysInMonth(String dateMM) {
        // 使用 DateTimeFormatter 来解析字符串为 YearMonth
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
        YearMonth yearMonth = YearMonth.parse(dateMM, formatter);

        // 获取并打印该月份的天数
        return yearMonth.lengthOfMonth();
    }

    @Override
    public List<WorkOrderTypes> getWorkOrderTypes(String dateMM) {
        int i = getDaysInMonth(dateMM);
        String dateStart = dateMM+"-01";
        String dateEnd = dateMM+"-"+(i);
        List<WorkOrderTypes> workOrderTypes = workOrderMetricsMapper.getWorkOrderTypes(dateStart, dateEnd);
        for (WorkOrderTypes workOrderType : workOrderTypes) {
            if (workOrderType.getTotalOrders() == 0){
                workOrderType.setWorkOrderOV(0.00);
            }else {
                double ij =(double) Math.round((workOrderType.getTotalOverdueOrders() / workOrderType.getTotalOrders())*100) ;
                workOrderType.setWorkOrderOV(ij);
            }
        }

        return workOrderTypes;


    }

    private List<WorkOrder> findAll(){
//        QueryWrapper<WorkOrder> queryWrapper = new QueryWrapper<>();
        return workOrderMapper.selectList(null);
    }





}
